<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/9/1
 * Time: 9:37
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Cost extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_cost')->count();// 获取总条数
            $lists = Db::name('match_cost')
                ->field('mc_id, mc_name, mc_time')
                ->page($Nowpage,$limits)
                ->order('mc_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加赛事费用
     * @return mixed|\think\response\Json
     */
    public function costAdd(){
        if(Request::instance()->isPost()){
            $mc_name = trim(input('mc_name'), ' ');
            $insert = [
                'mc_name' => $mc_name,
                'mc_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_match_cost')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('cost/costadd');
    }
    
    /**
     * 编辑赛事费用
     * @return mixed|\think\response\Json
     */
    public function costEdit(){
        $mc_id = input('mc_id');
        if(Request::instance()->isPost()){
            $mc_name = trim(input('mc_name'), ' ');
            $update = [
                'mc_name' => $mc_name,
                'mc_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_match_cost')->where('mc_id', $mc_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cost = Db::table('up_match_cost')
            ->where('mc_id', $mc_id)
            ->field('mc_name')
            ->find();
        $this->assign([
            'cost' => $cost,
            'mc_id' => $mc_id
        ]);
        return $this->fetch('cost/costedit');
    }
}